package com.syx.friend.Aop;

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.syx.commoncore.Constant.Constant;
import com.syx.commoncore.Utils.ThreadLocalUtil;
import com.syx.commoncore.enums.Resultcode;
import com.syx.friend.Manager.UserCacheManager;
import com.syx.friend.Model.user.VO.UserVO;
import com.syx.security.Exception.ServiceException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Aspect
@Component
public class UserStatusCheckAspect {

    @Autowired
    private UserCacheManager userCacheManager;

    @Before(value = "@annotation(com.syx.friend.Aop.CheckUserStatus)")
    public void before(JoinPoint point){
        Long userId = ThreadLocalUtil.get(Constant.USER_ID, Long.class);
        UserVO user = userCacheManager.getUserById(userId);

        if (user == null) {
            throw new ServiceException(Resultcode.FAILED_USER_NOT_EXISTS);
        }
        //已经拉黑
        if (Objects.equals(user.getStatus(), Constant.TRUE)) {
            throw new ServiceException(Resultcode.FAILED_USER_BANNED);
        }
    }

}